<?php
class Img_{
	static function image_resize($src, $dst="", $width=200, $height=200) {
		$dst = $dst == "" ? $src : $dst;
		if(!list($w, $h) = getimagesize($src))
			return "Unsupported picture type!";
	
		$type = strtolower(substr(strrchr($src, "."), 1));
		if($type == 'jpeg')
			$type = 'jpg';
		switch($type) {
			case 'bmp' :
				$img = imagecreatefromwbmp($src);
				break;
			case 'gif' :
				$img = imagecreatefromgif($src);
				break;
			case 'jpg' :
				$img = imagecreatefromjpeg($src);
				break;
			case 'png' :
				$img = imagecreatefrompng($src);
				break;
			default :
				return "Unsupported picture type!";
		}
		if(isset($_POST['x']) && isset($_POST['y']) && isset($_POST['h']) && isset($_POST['w'])) {
			$x = $_POST['x'];
			$y = $_POST['y'];
			$w = $width = $_POST['w'];
			$h = $height = $_POST['h'];
			unset($_POST['x']);
			unset($_POST['y']);
			unset($_POST['h']);
			unset($_POST['w']);
		}else{
			$x 	= 0;
			$y 	= 0;
			$w1 = $width;
			$h1 = $height;
			$width 	= $w;
			$height = $h;
			$w 	= $w1;
			$h 	= $h1;
		}
	
		$new = imagecreatetruecolor($w, $h);
	
		// preserve transparency
		if($type == "gif" or $type == "png") {
			imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
			imagealphablending($new, false);
			imagesavealpha($new, true);
		}
	
		imagecopyresampled($new, $img, 0, 0, $x, $y, $w, $h, $width, $height);
	
		switch($type) {
			case 'bmp' :
				imagewbmp($new, $dst);
				break;
			case 'gif' :
				imagegif($new, $dst);
				break;
			case 'jpg' :
				imagejpeg($new, $dst);
				break;
			case 'png' :
				imagepng($new, $dst);
				break;
		}
		return true;
	}
}

?>